﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TableLayoutDetectorEditor : Form, IComponentEditor
    {
        #region fields
        private object _ActivityNode;
        private TableLayoutDetectorComponent _LayoutDetectorComponent;
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public TableLayoutDetectorEditor()
        {
            InitializeComponent();
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof (TableLayoutDetectorComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._LayoutDetectorComponent = (TableLayoutDetectorComponent) wfComponent;

            this.txt_FileListXmlFilePath.Text = this._LayoutDetectorComponent.FileListInputXmlFilePath;
            this.txt_TableScanOutputFilePath.Text = this._LayoutDetectorComponent.TableScanResultFile;
            this.txt_LayoutOutputFilePath.Text = this._LayoutDetectorComponent.TableLayoutOutputFilePath;
            this.txt_LogFilePath.Text = this._LayoutDetectorComponent.LogFilePath;
            this.txt_ErrorFilePath.Text = this._LayoutDetectorComponent.ErrFilePath;
            this.num_ThreadPoolSize.Value = this._LayoutDetectorComponent.ThreadPoolSize;

            this.dv_ColHeaders.Rows.Clear();
            if(this._LayoutDetectorComponent.ColumnHeaderOptions !=null && this._LayoutDetectorComponent.ColumnHeaderOptions.Count>0)
            {
                foreach(int applyOrder in this._LayoutDetectorComponent.ColumnHeaderOptions.Keys)
                {
                    List<string> colHeaders = this._LayoutDetectorComponent.ColumnHeaderOptions[applyOrder];
                    DataGridViewRow dr=new DataGridViewRow();
                    for(int i=0;i<colHeaders.Count;i++)
                    {
                        DataGridViewTextBoxCell cellHeader=new DataGridViewTextBoxCell();
                        cellHeader.Value = colHeaders[i];
                        dr.Cells.Add(cellHeader);
                    }
                    this.dv_ColHeaders.Rows.Add(dr);
                }
            }

        }

        /// <summary></summary>
        public void SaveSettings()
        {
            this._LayoutDetectorComponent.FileListInputXmlFilePath = this.txt_FileListXmlFilePath.Text;
            this._LayoutDetectorComponent.TableScanResultFile = this.txt_TableScanOutputFilePath.Text;
            this._LayoutDetectorComponent.TableLayoutOutputFilePath = this.txt_LayoutOutputFilePath.Text;
            this._LayoutDetectorComponent.LogFilePath = this.txt_LogFilePath.Text;
            this._LayoutDetectorComponent.ErrFilePath = this.txt_ErrorFilePath.Text;
            this._LayoutDetectorComponent.ThreadPoolSize = (int) this.num_ThreadPoolSize.Value;

            this._LayoutDetectorComponent.ColumnHeaderOptions=new Dictionary<int, List<string>>();
            int applyOrder = 0;
            foreach(DataGridViewRow dr in this.dv_ColHeaders.Rows)
            {
                if(!dr.IsNewRow)
                {
                    applyOrder += 1;
                    List<string> colHeaders = new List<string>();
                    for(int i=0;i<this.dv_ColHeaders.Columns.Count;i++)
                    {
                        if(dr.Cells[i].Value !=null && dr.Cells[i].Value.ToString().Length>0)
                        {
                            colHeaders.Add(dr.Cells[i].Value.ToString());
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (colHeaders.Count > 0)
                    {
                        this._LayoutDetectorComponent.ColumnHeaderOptions.Add(applyOrder, colHeaders);
                    }
                }
            }
        }

        /// <summary></summary>
        public TestResult Test()
        {
            return new TestResult(true,"Success");
        }

        #endregion

        private void TableLayoutDetectorEditor_Load(object sender, EventArgs e)
        {

        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._ActivityNode, this._LayoutDetectorComponent);
            }
            this.Close();
        }

        private void btn_BrowseScanOutputFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableScanOutputFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BroseDocFolder_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_FileListXmlFilePath.Text = dlg.FileName;
            }
        }

        private void btn_SaveOutputTo_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_LayoutOutputFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseLog_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Log files(*.log)|*.log|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_LogFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseErr_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Err files(*.err)|*.err|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_ErrorFilePath.Text = dlg.FileName;
            }
        }
    }
}
